package com.charles.video.system.controller;

import com.charles.video.common.core.controller.BaseController;
import com.charles.video.common.core.responseDomain.AjaxResult;
import com.charles.video.common.core.responseDomain.TableInfo;
import com.charles.video.common.log.annotation.Log;
import com.charles.video.system.mapper.VideoInfoMapper;
import com.charles.video.system.mapper.VideoLogMapper;
import com.charles.video.system.service.VideoInfoService;
import com.charles.video.system.vo.SearchVideoForm;
import com.charles.video.system.vo.VideoDetailVo;
import lombok.extern.log4j.Log4j2;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author Charles-H
 * <p>
 * 视频控制层
 */
@RestController
@RequestMapping("/video")
@Log4j2
public class VideoController extends BaseController {

    @Resource
    private VideoInfoMapper videoInfoMapper;

    @Resource
    private VideoInfoService videoInfoService;

    @Resource
    private VideoLogMapper videoLogMapper;
    
    /**
     * 获取视频表格数据
     */
    @GetMapping("/getVideoTableData")
    public TableInfo getVideoTableData(Integer pageNum, Integer pageSize, SearchVideoForm searchVideoForm) {
        return videoInfoService.queryVideoTableData(pageNum, pageSize, searchVideoForm);
    }

    /**
     * 通过视频ID获取视频详情
     */
    @GetMapping("/getVideoDetail/{videoId}")
    public AjaxResult getVideoDetail(@PathVariable("videoId") String videoId) {
        return AjaxResult.success(videoInfoService.queryVideoDetail(videoId));
    }

    /**
     * 修改视频
     */
    @Log(behavior = "修改了一个视频信息")
    @PutMapping("/modifyVideo")
    public AjaxResult modifyVideo(@RequestBody VideoDetailVo videoDetailVo) {
        OkHttpClient httpClient = new OkHttpClient();

        if (videoDetailVo.getIsHide().equals("1") || videoDetailVo.getStatus().equals("2")) {
            Request request = new Request.Builder().url("http://127.0.0.1:8080/video/index/deleteVideoIndex/" + videoDetailVo.getVideoId())
                    .delete()
                    .build();

            // 使用异步方式发送请求
            httpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    log.info("异步删除索引失败:", e);
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    log.info("异步删除索引成功 ");
                    response.close();
                }
            });
        } else {
            // 异步存储索引
            Request request = new Request.Builder().url("http://127.0.0.1:8080/video/index/storeVideoIndex/" + videoDetailVo.getVideoId())
                    .post(okhttp3.RequestBody.create(null, new byte[0]))
                    .build();

            // 使用异步方式发送请求
            httpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    log.info("异步存储索引失败:", e);
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    log.info("异步存储索引成功 ");
                    response.close();
                }
            });
        }

        return toAjax(videoInfoMapper.modifyVideo(videoDetailVo));
    }

    /**
     * 删除视频
     */
    @Log(behavior = "删除了一个视频")
    @DeleteMapping("/deleteVideo/{videoId}")
    public AjaxResult deleteVideo(@PathVariable("videoId") String videoId) {
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder().url("http://127.0.0.1:8080/video/index/deleteVideoIndex/" + videoId)
                .delete()
                .build();

        // 使用异步方式发送请求
        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                log.info("异步删除索引失败:", e);
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                log.info("异步删除索引成功 ");
                response.close();
            }
        });
        return toAjax(videoInfoMapper.deleteVideo(videoId));
    }

    /**
     * TODO 还没想好改成什么
     * 行为记录日志
     */
    @GetMapping("/queryAllVideoLog")
    public TableInfo queryAllVideoLog() {
        startPage();
        return getDataTable(videoLogMapper.queryAllVideoLog());
    }
    
}
